 ;(function (mod){
	/**
	 * 获取参数
	 */
	 var getUrlParam =function(name){
	      var src = window.location.href;
	 	 var r = new RegExp("(\\?|#|&)" + name + "=([^&^#]*)(#|&|$)");
	      var m = src.match(r);
	      return !m ? "" : m[2];
	 }
	 var trim=	function (trimStr) {
	     return trimStr.replace(/(^\s*)|(\s*$)/g, "");
	 }   
	 var HtmlAttributeEncode = function(sStr){
		    sStr = sStr.replace(/&/g, "&amp;");
		    sStr = sStr.replace(/>/g, "&gt;");
		    sStr = sStr.replace(/</g, "&lt;");
		    sStr = sStr.replace(/"/g, "&quot;");
		    sStr = sStr.replace(/'/g, "&#39;");
		    sStr = sStr.replace(/=/g, "&#61;");
		    sStr = sStr.replace(/`/g, "&#96;");
		    return sStr;
		};
	 var HtmlAttributeDecode = function(sStr){
		    sStr = sStr.replace(/&amp;/g, "&");
		    sStr = sStr.replace(/&gt;/g, ">");
		    sStr = sStr.replace(/&lt;/g, "<");
		    sStr = sStr.replace(/&quot;/g, '"');
		    sStr = sStr.replace(/&#39;/g, "'");
		    sStr = sStr.replace(/&#96;/,"`" );
		    sStr = sStr.replace(/&#61;/,"=" );
		    return sStr;
		}		
	 /**
		 * eval function
		 */
	 var _eval=function(data){
		 return (new Function( "return " + data ))();
	 };
	 /**
		 * 如果是IE内核，转为XML格式
		 * 
		 * @param xml
		 * @returns string
		 */
	 function parseXml(xml) {
	 	// if (jQuery.browser.msie) {
	 	if (jQuery.support.leadingWhitespace == false) {
	 		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
	 		xmlDoc.loadXML(xml);
	 		xml = xmlDoc;
	 	}   
	 	return xml;
	 }
	/**
	 * 处理异常函数
	 */
	 var sys_errors=function(msg){
		 alert(msg);
		 return false;
	 };

    var _defaulrVal='请输入产品或问题',
     _faqDir="/faq/",
     _productID="NULL";
     _productDir="/product/";
	/**
	 * 显示FAQ列表
	 */
	 var _handleFAQ=function(faqList){
		 if(faqList.length==0){
			 return ;
		 }
		 var faqStr="";
		 for(var _faqKey in faqList){
			 _title= faqList[_faqKey].title,
			 _id=faqList[_faqKey].id,
			 _url=_faqDir+_id+".html",
			 _summary=faqList[_faqKey].content;
			faqStr+="<li ><a href='"+_url+"' target='_blank' onclick='KF.search.sta(this,\"click_faq_title\")'>"+_title+"</a><p>"+_summary+"</p></li>";
		 }
		 $("#faq_list").html(faqStr);
	 };
	 /**
		 * 显示四个工具信息
		 */
	 var _topFourList=function(toolsList,needDesc){
		 if($("#product_logo").is(":visible")==true){
			   $("#product_area").removeClass("related_tools_nopic");
		   }else{
			   $("#product_area").addClass("related_tools_nopic");
		   }
		 var str="";
		 if(needDesc==1 && typeof toolsList[0].tools_description !=="undefined"){
			 str="<p><span class='des'>"+toolsList[0].tools_description+"</span></p>";
		 }
		 var toolStr="<p class='btn_box'>";
		 for(var _toolKey in toolsList){
			 var _name=toolsList[_toolKey].tools_name;
			 var _url=toolsList[_toolKey].tools_url;
			 if(_name !=="" &&  _url!==""){
				 toolStr+="<a href='"+_url+"' target='_blank' onclick='KF.search.sta(this,\"click_top_tool\")'>"+_name+"</a>"; 
			 }
		 };
		 toolStr+="</p>";
        var _ori=$("#top_four_tool");
        _ori.html(_ori.html()+str+toolStr);
		 // $("#top_list").prepend(str).html(str+toolStr);
	 }
	 /**
		 * 产品专区
		 */
	 var _handleProduct=function(productList){
		 if(productList.length==0){
			 // return ;
		 }else{
			// $("#product_area").removeClass("related_tools_nopic");
		 }
		 var productStr="";
		 _productID=productList[0].product_code;
		 var _imc=productList[0].online_service_url;// IMC
		 var _productLogo=productList[0].product_logo;// LOGO
		 var _productName=productList[0].product_name;// 中文名
		 var _productEnName=productList[0].product_en_name;// 英文名
		 var _productStaticPah=_productDir+_productEnName+".html";
		 $("#product_logo")
		   .attr("src",_productLogo)
		   .attr("alt",_productName)
		   .click(function(){
			   KF.search.sta(this,"click_product_logo");
			 window.open(_productStaticPah);
		 }).show();// 产品LOGO
		 var _proUrl="<a  href='"+_productStaticPah+"' target='_blank' onclick='KF.search.sta(this,\"click_product_link\")'>进入<span class='key_red'>"+_productName+"</span>产品专区</a>";
		 $("#return_key").html(_proUrl+"");
		 $("#return_key").parent().append("<p><span class=\"des\">产品专区可以提供该产品相关问题的解决方案。</span></p>");
		 _topFourList(productList,0);
	 };
	 /**
		 * 工具的展示
		 */
     var _handleTools = function(tools) {
         if (tools.length == 0) {
             return;
         }
         if ($("#product_logo").is(":visible") == false && typeof tools[0].product_logo !== "undefined") {
             $("#product_logo")
                 .attr("src", tools[0].product_logo)
                 .attr("alt", tools[0].tools_name)
                 .click(function() {
                     KF.search.sta(this, "click_keys_logo");
                 }).show(); // 产品LOGO
         }
         var _searchKey = '<span class="key_red">' + tools[0].tools_name + '</span>'; // 搜素不是产品的时候，展示搜索关键字常用工具。不可点击
         $("#return_key").html("常用工具");
         _topFourList(tools, 1);
     };
	 /**
		 * 获得焦点,默认值轮换
		 */
	 var _focus=function(obj){
		 var _obj=$(obj);
		 if(_obj.val()==_defaulrVal){
			 _obj.attr("class",'tx_search_on');
			 _obj.val("");
				 }else{
			_obj.attr("class",'tx_search_on');
		   }
	 };
	 /**
		 * 点击,默认值轮换
		 */
	 var _click=function(obj){
		 var _obj=$(obj);
		 if(_obj.val()==_defaulrVal){
			 _obj.attr("class",'tx_search_on');
			 _obj.val("");
				 }else{
			_obj.attr("class",'tx_search_on');
		   }
	 };
	 /**
		 * onblur,默认值轮换
		 */
	 var _blur=function(obj){
		 var _obj=$(obj);
		 if(_obj.val()==''){
			 _obj.attr("class",'tx_search');
			 _obj.val(_defaulrVal);
				 }
	 };
	 /**
		 * 如果是回车直接搜索
		 */
	 var _down=function(evt,isLocalSearch){
		var evt = (evt) ? evt : ((window.event) ? window.event : "") // 兼容IE和Firefox获得keyBoardEvent对象
		var key = evt.keyCode?evt.keyCode:evt.which; // 兼容IE和Firefox获得keyBoardEvent对象的键值
		 if(key==13){
			   if(isLocalSearch=="1"){// 如果是在搜索界面内进行搜素
				   KF.search.handleSearch("inner");
			   }else{
				   KF.search.handleSearch(); 
			   }
			 }
	 };
	 var handleSearch=function(par){
		var _searchKey="";
	    var URL = "http://kf.qq.com/search_result.html";
	    if(par==='inner'){// 如果是在搜索本界面内的搜索框
	    	_searchKey=($("#current_search_key").val()) ;
	    }else{
	    	_searchKey =($("#kf_search_key").val()) ;
	    }
	    if(_defaulrVal===_searchKey){
	    	_searchKey="";
	    }
	    var sCon = HtmlAttributeEncode(_searchKey);     
	    sCon = trim(sCon);
	    if ( sCon.length < 1)
	    {
	        // alert("请输入要搜索的关键字!");
	       // return false;
	}
	    URL += "?search_key=" + HtmlAttributeDecode(encodeURIComponent(sCon) )+ "";
	   window.location.href = URL ;
	 }; 	
	 var _renderSOSOList=function(sosoList){
		var _total= $(sosoList).find("totalNum").text();
		_total=parseInt(_total);
	   if(_total==0){
			 $("#soso_container").hide();
			 return false;
		 }
	   var _str="";
	 $(sosoList).find("question").each(function(item){
		 var _this=$(this);
		 var _link="http://wenwen.soso.com/z/q"+_this .find("id").text()+".htm";
		 var _title=_this .find("title").text();
		 var _answer=_this .find("answerNum").text();
		 _str+="<li><a href="+_link+" class='tlt' target='_blank' onclick='KF.search.sta(this,\"click_wenwen_link\")' >"+_title+"</a><span class='num'>"+_answer+"个回答</span></li>";
	 });
		 $("#soso_list").append(_str);
		 $("#soso_container").show();
	 };
	 var _searchBySOSO=function(_searchKey){
		 $.ajax({
			 type:"GET",
			 url:"/cgi-bin/common?rand="+Math.random(),
			 data:"command=" +encodeURIComponent('command=C00025&type=WenWenSearch&query='+_searchKey),
			 dataType:"json"
		 }).done(function(msg){
			  var _temp=(msg.resultinfo.list[0].content);
			 var _tt= parseXml(decodeURIComponent(_temp));
			 _renderSOSOList(_tt);
		 });
	 }
	 var _searchByKF = function(_searchKey) {
	     $.ajax({
	         type: "GET",
	         url: "/cgi-bin/common?rand=" + Math.random(),
	         data: "command=" + encodeURIComponent('command=C00032&commandtype=webSearch&systype=1008&query=' + _searchKey),
	         dataType: "json",
	         timeout: 5000,
	         success: function(jsonobj) {
	             if (jsonobj.resultcode != 0 || jsonobj.resultinfo.list[0] == undefined) {
	                 return sys_errors("服务器繁忙，请您稍候再试");
	             }
	             var _list = jsonobj.resultinfo.list[0].list;
	             _list = decodeURIComponent(_list);

	             try {
	                 var _rs = _eval(_list);
	             } catch(err) {
	                 return sys_errors("服务器繁忙，请您稍候再试"); // TODO log error info
	             }

	             var _faq = _rs.docs;
	             var _product = _rs.productTools;
	             var _tools = _rs.tools;
	             if (_faq.length === 0 && _product.length === 0 && _tools.length === 0) { // 没有搜索结果
	                 $("#product_area").hide();
	                 $("#no_search_rs").show();
	                 return;
	             }
	             if (_faq.length > 0) {
	                 _handleFAQ(_faq);
	             }
	             if (_product.length > 0) {
	                 $("#product_area").removeClass("related_tools_nopic"); // 显示产品LOGO
	                 _handleProduct(_product);
	             } else {
	                 $("#product_area").addClass("related_tools_nopic"); // 隐藏产品LOGO
	             }

	             if (_product.length == 0 && _tools.length == 0) { // 没有产品也没有工具的时候，将DIV隐藏
	                 $("#product_area").hide();
	             }
	             if (_product.length < 4 && _tools.length > 0) { // 如果产品专区有了四个工具
	                 _handleTools(_tools); // 拉取模糊工具中的信息
	             }

	         },
	         error: function(request, msg, err) {
	             return sys_errors("服务器繁忙，请您稍候再试");
	         }
	     });
	 }
     var getSearchList=function(){
		 var _searchKey=getUrlParam('search_key');
		 if(_searchKey.length==0){
			 // alert("请输入关键字");
		 };
		 var _decodeKey=decodeURIComponent(_searchKey);
		 _decodeKey=HtmlAttributeDecode(_decodeKey);
		 if(_decodeKey.length==0){
			 _decodeKey=_defaulrVal;
			 // alert("请输入关键字");
		 }else{
			 $("#current_search_key").val(_decodeKey);
		 }
		 $("#kf_search_key").val(_decodeKey);// 搜索框获取当前的输入字段
		 _searchByKF(_searchKey);
		 _searchBySOSO(_searchKey);  
	 };

	 /**
		 * 统计信息
		 */
	 var _sta=function(obj,atr){
		var _key= $("#current_search_key").val();
		var   _title=$(obj).text()!=""?$(obj).text():$(obj).attr("alt");
		var    _flag=atr;
		_key = _key.replace(/,/g, "，");
		_title= _title.replace(/,/g, "，");
		 var _command=_key+","+_flag+","+_title+","+_productID;
		 _command=encodeURIComponent(_command);
			$.ajax({
				type:"POST",
				url:"/cgi-bin/statisticsComm",
				contentType: 'application/x-www-form-urlencoded; charset=UTF-8', 
				data:'flagInfo=KfWebSearch&statisticsInfo='+_command+"&ran="+Math.random(),
				dataType:"json"
			});	
	 };
	 mod.search=(function(){
return {
	getSearchList : getSearchList,
	handleSearch : handleSearch,
	focus : _focus,
	click : _click,
	blur : _blur,
	keydown: _down,
	sta : _sta
		};
   })();
　　　　return mod;
 })(window.KF || {});


/* 下为智能提示的内容 by marvinliang */

 Search = window.Search || {};

 Search.Suggest = (function () {

     var myHandler = function () {
         var activeId = document.activeElement.id; //当前输入框id

         if (activeId === "") {
             return false;
         }

         var keyCode;
         if (arguments[0]) {
             keyCode = arguments[0].keyCode;
         } else {
             event = event || Event;
             keyCode = event.keyCode;
         }
         var bgColor = "#d1e5fc";
         var hiddenId = activeId + "_hidden"; //隐藏框
         var suggestId = activeId + "_suggest"; //提示结果
         var inputObj = document.getElementById(activeId);
         var hiddenObj = document.getElementById(hiddenId);
         var suggestObj = document.getElementById(suggestId);

         if (inputObj == null || hiddenObj == null || suggestObj == null) {
             return false;
         }

         //输入空
         if (!Search.Suggest.checkNullInput(inputObj)) {
             hiddenObj.value = inputObj.value;
             suggestObj.style.display = "none";
             return false;
         }
         //鼠标下移
         if (keyCode == 40) {
             if (suggestObj.style.display == "block") {
                 Search.Suggest.moveUpDown(suggestObj, "down", bgColor);
             }
             return false;
         }
         //鼠标上移
         if (keyCode == 38) {
             if (suggestObj.style.display == "block") {
                 Search.Suggest.moveUpDown(suggestObj, "up", bgColor);
             }
             return false;
         }

         //如果输入没有变化返回
         if ($.trim(inputObj.value) === $.trim(hiddenObj.value)) {
             return false;
         }

         //获得提示列表
         if (keyCode != 116) {
             $(inputObj).blur(function () {
                 suggestObj.style.display = "none";
             });
             Search.Suggest.getSuggestItem(inputObj, hiddenObj, suggestObj);

             hiddenObj.value = inputObj.value;
             return true;
         }
     }


     var resultItemClick = function (e) {
         document.location.href = "/search_result.html?search_key=" + $(e).text();
     }

     var itemOnMouseOver = function (e) {
         //var inputObj = $(e).parent().parent().parent().parent().find("input[SearchSuggest=true]");
         var mySuggest = $(e).closest(".kf_mod_search_associate");
         var inputObj = mySuggest.parent().find("input[SearchSuggest=true]");
         if (inputObj) {
             $(inputObj).unbind("blur");
         }
     }
     var itemOnMouseOut = function (e) {
         var mySuggest = $(e).closest(".kf_mod_search_associate");
         var inputObj = mySuggest.parent().find("input[SearchSuggest=true]");
         if (inputObj) {
             inputObj.blur(function () {
                 mySuggest.css("display", "none");
             });
         }

     }

     var getSuggestItem = function (inputObj, hiddenObj, suggestObj) {
         //如果输入没有变化返回
         if ($.trim(inputObj.value) === $.trim(hiddenObj.value)) {
             return false;
         }
         //输入空
         if (!Search.Suggest.checkNullInput(inputObj)) {
             return false;
         }

         var cmd = 'command=C00057&input1=getSuggestWord&input3=json&fromtype=wss&input9=wss&input5=' + $.trim(inputObj.value);

         $.ajax({
             type: "GET",
             url: "/cgi-bin/common?rand=" + Math.random(),
             data: "command=" + encodeURIComponent(cmd),
             timeout: 500,
             success: function (msg) {
                 try {
                     var res = $.parseJSON(msg);
                     var _rs = res.resultcode;
                     if (_rs == 0) { //查询成功
                         var innerHtmlStr = "";
                         var infos = decodeURIComponent(res.resultinfo.list[0].info);
                         var listRes = $.parseJSON(infos);
                         //产品专区
                         if (listRes.products != null && listRes.products.length > 0) {
                             var itemsCount = 3;
                             if (listRes.products.length < itemsCount) {
                                 itemsCount = listRes.products.length;
                             }
                             for (var i = 0; i < itemsCount; i++) {
                                 innerHtmlStr += "<h3 class=\"section_asso\"><a href=\"#\" onmouseover=\" Search.Suggest.itemOnMouseOver(this) \"  onmouseout=\"  Search.Suggest.itemOnMouseOut(this) \"  onclick=\"Search.Suggest.resultItemClick(this);\">" + listRes.products[i].product_name + "</a></h3>";
                             }
                         }

                         //热点faq
                         if (listRes.faqs != null && listRes.faqs.length > 0) {
                             var itemsCount = 10;
                             if (listRes.faqs.length < itemsCount) {
                                 itemsCount = listRes.faqs.length;
                             }
                             innerHtmlStr += "<ul>";
                             for (var i = 0; i < itemsCount; i++) {
                                 innerHtmlStr += "<li><a href=\"#\" onmouseover=\" Search.Suggest.itemOnMouseOver(this) \"  onmouseout=\"  Search.Suggest.itemOnMouseOut(this) \"  onclick=\"Search.Suggest.resultItemClick(this);\" >" + listRes.faqs[i].title + "</a></li>";
                             }
                             innerHtmlStr += "</ul>";
                         }
                         if (innerHtmlStr != "") {
                             $(suggestObj).html(innerHtmlStr);
                             suggestObj.style.display = "block";
                         }
                     }
                 } catch (e) {
                     var a = e;
                 }
             },
             error: function (err) {
                 //alert(err);
             }
         });


     }


     var checkNullInput = function (inputObj) {
         var nullInputText = '请输入产品或问题';
         if ($.trim(inputObj.value) == "" || $.trim(inputObj.value) == nullInputText) {
             return false;
         } else {
             return true;
         }
     }
     //键盘上下键控制
     var moveUpDown = function (suggestObj, direction, bgColor) {
         var objA = suggestObj.getElementsByTagName("a");
         var avtiveElement=$(document.activeElement);
         var flagA = 100;
         for (var i = 0; i < objA.length; i++) {
             if (objA[i].style.backgroundColor != "") {
                 flagA = i;
                 objA[i].style.backgroundColor = "";
             }
         }

         if (flagA == 100) {
             objA[0].style.backgroundColor = bgColor;
             avtiveElement.val($(objA[0]).text());
         } else {
             if (direction == "up") {
                 if (flagA - 1 > -1) {
                     objA[flagA - 1].style.backgroundColor = bgColor;
                    avtiveElement.val($(objA[flagA - 1]).text());
                 } else {
                     objA[0].style.backgroundColor = bgColor;
                    avtiveElement.val($(objA[0]).text());
                 }
             } else if (direction == "down") {
                 if (flagA + 1 < objA.length) {
                     objA[flagA + 1].style.backgroundColor = bgColor;
                    avtiveElement.val($(objA[flagA + 1]).text());
                 } else {
                     objA[objA.length - 1].style.backgroundColor = bgColor;
                    avtiveElement.val($(objA[flagA + 1]).text());
                 }
             }
         }

     }


     var init = function () {
         try {
             if (document.addEventListener) {
                 document.addEventListener("keyup", Search.Suggest.myHandler, false);
             } else {
                 document.attachEvent("onkeyup", Search.Suggest.myHandler);
             }
         } catch (e) {
         }
     }

     return {
         myHandler: myHandler,
         init: init,
         itemOnMouseOver: itemOnMouseOver,
         itemOnMouseOut: itemOnMouseOut,
         getSuggestItem: getSuggestItem,
         checkNullInput: checkNullInput,
         resultItemClick: resultItemClick,
         moveUpDown: moveUpDown
     }

 })();

 $(function () {
     Search.Suggest.init();
 });